#!/bin/sh

# backupccdata - Backup critical console data to an archive file
#
# Usage: backupccdata <archive> <tempdir> <logfile> <actbrst> <build>
# This script must be run by a user with appropriate privileges (e.g., root)
#
# archive: the filename of the backup archive to create
# tempdir: the name of the directory to contain temporary/work files
# logfile: the filename of the log file to create
# actbrst: the filename of the actbrst file
# build:   the build identifier
#
# Return Codes:
# 1 - tar error

ARCHIVE=$1
TMPDIR=$2
LOG=/console/$3
ACTBRST=$4
BUILD=$5

# Start log
echo -e "-> backupccdata\n" > $LOG
echo -e "Backup log for `date`\n" >> $LOG

# Find files and add them to tarball

builddate=`date --file=$BUILD +%s`

currdate=`date +%s`

let "elapsedsecs = currdate - builddate"

let "elapsedtime = elapsedsecs/60"

let "remsecs = elapsedsecs%60"

if [ ! $remsecs = 0 ]; then
    let "elapsedtime+=1"
fi
pwd >> $LOG
echo "$elapsedtime" >> $LOG
cd /
echo -e "just did the ch dir /n" >>$LOG
echo $BUILD >> $LOG
# change /backup to / when doing real backups
# find / -xdev -type f -mmin -$elapsedtime -print > $TMPDIR/filelist
# find -cnewer /console/data/builddate -type f -print >$TMPDIR/bubba
find -cnewer $BUILD -type f -print >$TMPDIR/filelist
# cd /console
pwd >> $LOG
echo -e "just did the newer thang/n" >>$LOG
# special file that might not be backed up if we don't explicitly add this to the script
echo ".$ACTBRST" >> $TMPDIR/filelist
echo -e "jus added the actbrst.trm file to filelist/n">>$LOG
echo "$ARCHIVE" >> $LOG
echo "$ACTBRST" >> $LOG
echo -e "now let'z do the tar - zan, Jane!/n" >>$LOG
cd /
# create new archive
if !(tar -cvjPf $ARCHIVE --exclude-from=$TMPDIR/exfiles --files-from=$TMPDIR/filelist --ignore-failed-read >> $LOG 2>&1); then # floppy test
    echo -e "tar error.. exiting\n" >> $LOG
    exit 1
fi
cd /console
echo -e "<- backupccdata\n" >> $LOG
